package cn.rumoss.jvm.deep.oom;

import java.lang.reflect.Field;

import sun.misc.Unsafe;

/**
 * -Xmx10M -XX:MaxDirectMemorySize=5M
 * @author xiaotang
 *
 */
public class DirectMemoryOOM {

	private static final int _1MB = 1024*1024;
	
	public static void main(String[] args) throws Exception {
		
		Field unsafeField = Unsafe.class.getDeclaredFields()[0];
		unsafeField.setAccessible(true);
		Unsafe unsafe = (Unsafe) unsafeField.get(null);
		int idx = 0;
		while(true) {
			unsafe.allocateMemory(_1MB);// 真正分配内存
			System.out.println(++idx);
		}

	}

}
